/**
 * Created by huachuan on 2016/1/17.
 */
angular.module('lbsService',[])
  //约局的Service
  .factory('LBS', function($resource,$rootScope,ENV,Toast){

    //访问的地址
    var APIUrl = ENV.api;
    var location = {longitude:113.23,latitude:23.16};
    var cityListJson,cityJson,hotCityJson,hotCityName;
    var lbsResource = $resource(APIUrl+'lbs/getCityList.do');
    var getCityByIdResource = $resource(APIUrl+'lbs/getCityById.do');

    return{
      //启用定位功能
      getLocation:function(){
        return location;
      },
      getPosition:function(){
        document.addEventListener("deviceready", function(){
          if(ionic.Platform.isAndroid()){//安卓
            window.locationService.getCurrentPosition(function(position){
              location = {
                longitude:position.coords.longitude,
                latitude:position.coords.latitude
              };
              $rootScope.$broadcast('LBS.locationSuccess');
              window.locationService.stop();
            },function(position){
              location = {
                longitude:position.coords.longitude,
                latitude:position.coords.latitude
              };
              $rootScope.$broadcast('LBS.locationSuccess');
              window.locationService.stop();
            });
            navigator.geolocation.getCurrentPosition(function(position){
                location = {
                  longitude:position.coords.longitude,
                  latitude:position.coords.latitude
                };
                $rootScope.$broadcast('LBS.locationSuccess');
              },
              function(error){
              },{ timeout: 10000, enableHighAccuracy: true });
           /* var geolocation = new BMap.Geolocation();
            geolocation.getCurrentPosition(function(position){
              if(this.getStatus() == BMAP_STATUS_SUCCESS){
                location = {
                  longitude:position.point.lng,
                  latitude:position.point.lat
                };
                $rootScope.$broadcast('LBS.locationSuccess');
                return true;
              }
            });*/
          } else if(ionic.Platform.isIOS()){//IOS
            navigator.geolocation.getCurrentPosition(function(position){
                location = {
                  longitude:position.coords.longitude,
                  latitude:position.coords.latitude
                };
                $rootScope.$broadcast('LBS.locationSuccess');
              },
              function(error){
                //Toast.showMessage("定位当前位置失败,请允许该应用使用定位功能");
              },{ timeout: 10000, enableHighAccuracy: true });
          }
        }, false);
      },

      setLocationCityName:function(name){
        hotCityName = name;
      },
      getLocationCityName:function(){
        return hotCityName;
      },
      getHotCityByName:function(cityName){
        getCityByIdResource.save({
          city_name:cityName
        },function(response){
          hotCityJson = response;
          $rootScope.$broadcast('LBS.getHotCityJsonUpdated');
        });
      },
      getHotCityJson:function(){
          return hotCityJson;
      },
      getCityByName:function(cityName){
        getCityByIdResource.save({
          city_name:cityName
        },function(response){
          cityJson = response;
          $rootScope.$broadcast('LBS.getCityJsonUpdated');
        });
      },
      //根城市编号获取城市信息
      getCityById:function(cityId){
        getCityByIdResource.save({
          shop_city_id:cityId
        },function(response){
          cityJson = response;
          $rootScope.$broadcast('LBS.getCityJsonUpdated');
        });
      },
      getCityJson:function(){
        return cityJson;
      },
      getCityList:function(){
        lbsResource.save({
          member_id:1
        },function(response){
          cityListJson = response;
          $rootScope.$broadcast('LBS.getCityListUpdated');
        });
      },
      getCityListJson:function(){
        return cityListJson;
      }
    }
  })
